package com.mytijian.controller;

import com.alibaba.fastjson.JSON;
import com.mytijian.code.dto.MashangBaseResult;
import com.mytijian.code.param.ModifyValidityParam;
import com.mytijian.code.param.alihalt.ToTBAvailableParam;
import com.mytijian.code.param.alihalt.ToTBConsumeParam;
import com.mytijian.code.service.ConsumeCodeService;
import com.mytijian.runtime.ServiceExecutorFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
public class CodeMerchantController {

    @Autowired
    private ServiceExecutorFacade serviceExecutorFacade;
//    @Autowired
//    private CallbackService callbackService;
    @Autowired
    private ConsumeCodeService consumeCodeService;

    @RequestMapping(value = "/code/test")
    public void test(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            serviceExecutorFacade.executeAliCodeMchService(request, response);
        } catch (Exception ex) {
            MashangBaseResult result = new MashangBaseResult();
            result.setCode("isv.fail-send-no-stock");
            result.setMsg(ex.getMessage());
            response.getWriter().write(JSON.toJSONString(result));
        }
    }

    @RequestMapping(value = "/code/available/{code}")
    public void availableTest(@PathVariable("code") String code) {
        ToTBAvailableParam request = new ToTBAvailableParam();
        request.setCode(code);
        consumeCodeService.available(request);
    }

    @RequestMapping(value = "/code/consume/{code}")
    public void consumeTest(@PathVariable("code") String code) {
        ToTBConsumeParam request = new ToTBConsumeParam();
        request.setCode(code);
        consumeCodeService.consume(request);
    }

    @RequestMapping(value = "/code/invalid")
    public void acceptInvalidTest() {
//        InvalidParam param = new InvalidParam();
//        param.setOuterId("61259260095776028");
//        mcodeService.acceptInvalid(param);
    }

    @RequestMapping(value = "/code/modifyValidity")
    public void acceptModifyValidityTest() {
        ModifyValidityParam param = new ModifyValidityParam();
        param.setOuterId("61259260095776028");
        param.setValidStart("2017-09-23 00:00:00");
        param.setValidEnd("2017-09-24 23:59:59");
//        mcodeService.acceptModifyValidity(param);
    }

    @RequestMapping(value = "/code/acceptModifyValidity")
    public void acceptModifyValidity() {
        ModifyValidityParam param = new ModifyValidityParam();
        param.setOuterId("62088844466776028");
        param.setValidStart("2017-09-25 00:00:00");
        param.setValidEnd("2017-09-27 23:59:59");
        param.setBizType("3001");
        param.setToken("8c9d4262473295de74efb3fdce86d857");
        param.setOrganizerId("2928278102");
        param.setOrganizerNick("阿里健康大药房");
        param.setItemId("554919906396");
        param.setItemTitle("每天健康 &quot;测试商品&quot; 常规体检套餐  &quot;请不要拍&quot;");
//        mcodeService.acceptModifyValidity(param);
    }

}
